rm(list = ls())
source("code/_preamble.R")

#________________________________
##Load the dataset
load("data/weekly_attack.RData")


#________________________________
##FIGURE 1

#________________________________
#general attack
out_general <- att_gt(yname = "attacks_binary",
                      gname = "first_treat",
                      idname = "town_num",
                      tname = "week",
                      xformla = ~ 1,
                      data = elections[elections$to_keep == 1,],
                      est_method = "reg",
                      clustervars = "town_num"
)

es_simple_general <- aggte(out_general, type = "simple", clustervars = "town_num")
es_dynamic_general <- aggte(out_general, type = "dynamic", clustervars = "town_num")

#________________________________
#rebels attack
out_rebels <- att_gt(yname = "rebels_binary",
                     gname = "first_treat",
                     idname = "town_num",
                     tname = "week",
                     xformla = ~ 1,
                     data = elections[elections$to_keep == 1,],
                     est_method = "reg",
                     clustervars = "town_num"
)

es_simple_rebels <- aggte(out_rebels, type = "simple", clustervars = "town_num")
es_dynamic_rebels <- aggte(out_rebels, type = "dynamic", clustervars = "town_num")

#________________________________
#gov attack
out_gov <- att_gt(yname = "government_binary",
                  gname = "first_treat",
                  idname = "town_num",
                  tname = "week",
                  xformla = ~ 1,
                  data = elections[elections$to_keep == 1,],
                  est_method = "reg",
                  clustervars = "town_num"
)

es_simple_gov <- aggte(out_gov, type = "simple", clustervars = "town_num")
es_dynamic_gov <- aggte(out_gov, type = "dynamic", clustervars = "town_num")

#________________________________
#dataset to plot
to_plot <- data.frame(effect = c(es_dynamic_general$att.egt, 
                                 es_dynamic_rebels$att.egt,
                                 es_dynamic_gov$att.egt),
                      se = c(es_dynamic_general$se.egt, 
                             es_dynamic_rebels$se.egt,
                             es_dynamic_gov$se.egt),
                      attack = c(rep("Attacks total", 29), rep("Attacks by rebels", 29), rep("Attacks by the government", 29)),
                      type = rep(c(rep("Pre", 17), rep("Post", 12)), 3),
                      time  = rep(-17:11, 3))

to_plot$upper <- to_plot$effect + to_plot$se*1.96
to_plot$lower <- to_plot$effect - to_plot$se*1.96
to_plot$type <- as.factor(to_plot$type)
to_plot$type <- relevel(to_plot$type, ref = 2)

#________________________________
#The figure
ggplot(to_plot[to_plot$time > -12,], aes(time, effect, color = type)) + 
  geom_point() +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0) +
  geom_hline(yintercept = 0) + 
  theme_bw() +
  labs(x = "", y = "") + 
  theme(axis.text.y =  element_text(size = 11), axis.text.x = element_text(size = 11), 
        axis.title.y =  element_text(size = 13), axis.title.x = element_text(size = 13),
        strip.text.x = element_text(size = 13),
        strip.background = element_rect(fill='grey', color='white'),
        legend.position = "bottom", legend.title = element_blank()) +
  scale_x_continuous(breaks = -11:11) + 
  scale_color_manual(values = c("red", "blue")) +
  facet_wrap(~ attack, ncol = 1)




res_control <- c(round(es_simple_rebels$overall.att, 3), str_c("(", (round(es_simple_rebels$overall.se, 3)), ")"),
                 round(es_simple_gov$overall.att, 3), str_c("(", (round(es_simple_gov$overall.se, 3)), ")"),
                 round(es_simple_general$overall.att, 3), str_c("(", (round(es_simple_general$overall.se, 3)), ")"))


#________________________________
##FIGURE A2
rm(list = setdiff(ls(), c("elections", "res_control")))

#________________________________
#general attack
out_general <- att_gt(yname = "attacks_binary",
                      gname = "first_treat",
                      idname = "town_num",
                      tname = "week",
                      xformla = ~ 1,
                      data = elections[elections$to_keep_dur == 1,],
                      est_method = "reg",
                      clustervars = "town_num"
)

es_simple_general <- aggte(out_general, type = "simple", clustervars = "town_num")
es_dynamic_general <- aggte(out_general, type = "dynamic", clustervars = "town_num")

#________________________________
##rebels attack
out_rebels <- att_gt(yname = "rebels_binary",
                     gname = "first_treat",
                     idname = "town_num",
                     tname = "week",
                     xformla = ~ 1,
                     data = elections[elections$to_keep_dur == 1,],
                     est_method = "reg",
                     clustervars = "town_num"
)

es_simple_rebels <- aggte(out_rebels, type = "simple", clustervars = "town_num")
es_dynamic_rebels <- aggte(out_rebels, type = "dynamic", clustervars = "town_num")

#________________________________
##gov attack
out_gov <- att_gt(yname = "government_binary",
                  gname = "first_treat",
                  idname = "town_num",
                  tname = "week",
                  xformla = ~ 1,
                  data = elections[elections$to_keep_dur == 1,],
                  est_method = "reg",
                  clustervars = "town_num"
)

es_simple_gov <- aggte(out_gov, type = "simple", clustervars = "town_num")
es_dynamic_gov <- aggte(out_gov, type = "dynamic", clustervars = "town_num")

#________________________________
##get the dataset to plot
to_plot <- data.frame(effect = c(es_dynamic_general$att.egt, 
                                 es_dynamic_rebels$att.egt,
                                 es_dynamic_gov$att.egt),
                      se = c(es_dynamic_general$se.egt, 
                             es_dynamic_rebels$se.egt,
                             es_dynamic_gov$se.egt),
                      attack = c(rep("Attacks total", 29), rep("Attacks by rebels", 29), rep("Attacks by the government", 29)),
                      type = rep(c(rep("Pre", 17), rep("Post", 12)), 3),
                      time  = rep(-17:11, 3))

to_plot$upper <- to_plot$effect + to_plot$se*1.96
to_plot$lower <- to_plot$effect - to_plot$se*1.96
to_plot$type <- as.factor(to_plot$type)
to_plot$type <- relevel(to_plot$type, ref = 2)

#________________________________
##The figure
ggplot(to_plot[to_plot$time > -12,], aes(time, effect, color = type)) + 
  geom_point() +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0) +
  geom_hline(yintercept = 0) + 
  theme_bw() +
  labs(x = "", y = "") + 
  theme(axis.text.y =  element_text(size = 11), axis.text.x = element_text(size = 11), 
        axis.title.y =  element_text(size = 13), axis.title.x = element_text(size = 13),
        strip.text.x = element_text(size = 13),
        strip.background = element_rect(fill='grey', color='white'),
        legend.position = "bottom", legend.title = element_blank()) +
  scale_x_continuous(breaks = -11:11) + 
  scale_color_manual(values = c("red", "blue")) +
  facet_wrap(~ attack, ncol = 1)

res_dur <- c(round(es_simple_rebels$overall.att, 3), str_c("(", (round(es_simple_rebels$overall.se, 3)), ")"),
                 round(es_simple_gov$overall.att, 3), str_c("(", (round(es_simple_gov$overall.se, 3)), ")"),
                 round(es_simple_general$overall.att, 3), str_c("(", (round(es_simple_general$overall.se, 3)), ")"))


#________________________________
##FIGURE A1
rm(list = setdiff(ls(), c("elections", "res_control", "res_dur")))

#________________________________
##general attack
out_general <- att_gt(yname = "attacks_binary",
                      gname = "first_treat",
                      idname = "town_num",
                      tname = "week",
                      xformla = ~ 1,
                      data = elections,
                      est_method = "reg",
                      clustervars = "town_num"
)

es_simple_general <- aggte(out_general, type = "simple", clustervars = "town_num")
es_dynamic_general <- aggte(out_general, type = "dynamic", clustervars = "town_num")

#________________________________
##rebels attack
out_rebels <- att_gt(yname = "rebels_binary",
                     gname = "first_treat",
                     idname = "town_num",
                     tname = "week",
                     xformla = ~ 1,
                     data = elections,
                     est_method = "reg",
                     clustervars = "town_num"
)

es_simple_rebels <- aggte(out_rebels, type = "simple", clustervars = "town_num")
es_dynamic_rebels <- aggte(out_rebels, type = "dynamic", clustervars = "town_num")

#________________________________
##gov attack
out_gov <- att_gt(yname = "government_binary",
                  gname = "first_treat",
                  idname = "town_num",
                  tname = "week",
                  xformla = ~ 1,
                  data = elections,
                  est_method = "reg",
                  clustervars = "town_num"
)

es_simple_gov <- aggte(out_gov, type = "simple", clustervars = "town_num")
es_dynamic_gov <- aggte(out_gov, type = "dynamic", clustervars = "town_num")

#________________________________
##dataset to plot
to_plot <- data.frame(effect = c(es_dynamic_general$att.egt, 
                                 es_dynamic_rebels$att.egt,
                                 es_dynamic_gov$att.egt),
                      se = c(es_dynamic_general$se.egt, 
                             es_dynamic_rebels$se.egt,
                             es_dynamic_gov$se.egt),
                      attack = c(rep("Attacks total", 29), rep("Attacks by rebels", 29), rep("Attacks by the government", 29)),
                      type = rep(c(rep("Pre", 17), rep("Post", 12)), 3),
                      time  = rep(-17:11, 3))

to_plot$upper <- to_plot$effect + to_plot$se*1.96
to_plot$lower <- to_plot$effect - to_plot$se*1.96
to_plot$type <- as.factor(to_plot$type)
to_plot$type <- relevel(to_plot$type, ref = 2)

#________________________________
##The Figure
ggplot(to_plot[to_plot$time > -12,], aes(time, effect, color = type)) + 
  geom_point() +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0) +
  geom_hline(yintercept = 0) + 
  theme_bw() +
  labs(x = "", y = "") + 
  theme(axis.text.y =  element_text(size = 11), axis.text.x = element_text(size = 11), 
        axis.title.y =  element_text(size = 13), axis.title.x = element_text(size = 13),
        strip.text.x = element_text(size = 13),
        strip.background = element_rect(fill='grey', color='white'),
        legend.position = "bottom", legend.title = element_blank()) +
  scale_x_continuous(breaks = -11:11) + 
  scale_color_manual(values = c("red", "blue")) +
  facet_wrap(~ attack, ncol = 1)


res_general <- c(round(es_simple_rebels$overall.att, 3), str_c("(", (round(es_simple_rebels$overall.se, 3)), ")"),
                 round(es_simple_gov$overall.att, 3), str_c("(", (round(es_simple_gov$overall.se, 3)), ")"),
                 round(es_simple_general$overall.att, 3), str_c("(", (round(es_simple_general$overall.se, 3)), ")"))

#________________________________
##TABLE 2
to_print <- cbind(res_general, res_control, res_dur)
to_print <- rbind(to_print, c(nrow(elections), nrow(elections[elections$to_keep == 1,]), nrow(elections[elections$to_keep_dur == 1,])))
print(xtable(to_print), include.rownames = F)


#_______________________________________________________________
##TABLE A7
rm(list = ls())
#________________________________
##Load the dataset
load("data/weekly_attack.RData")

#________________________________
##Exclude the curfew week
elections <- filter(elections, !week == first_treat)

#________________________________
#general attack
out_general <- att_gt(yname = "attacks_binary",
                      gname = "first_treat",
                      idname = "town_num",
                      tname = "week",
                      xformla = ~ 1,
                      data = elections[elections$to_keep == 1,],
                      est_method = "reg",
                      clustervars = "town_num",
                      allow_unbalanced_panel = T
)

es_simple_general <- aggte(out_general, type = "simple", clustervars = "town_num", na.rm = T)

#________________________________
#rebels attack
out_rebels <- att_gt(yname = "rebels_binary",
                     gname = "first_treat",
                     idname = "town_num",
                     tname = "week",
                     xformla = ~ 1,
                     data = elections[elections$to_keep == 1,],
                     est_method = "reg",
                     clustervars = "town_num",
                     allow_unbalanced_panel = T
)

es_simple_rebels <- aggte(out_rebels, type = "simple", clustervars = "town_num", na.rm = T)

#________________________________
#gov attack
out_gov <- att_gt(yname = "government_binary",
                  gname = "first_treat",
                  idname = "town_num",
                  tname = "week",
                  xformla = ~ 1,
                  data = elections[elections$to_keep == 1,],
                  est_method = "reg",
                  clustervars = "town_num",
                  allow_unbalanced_panel = T
)

es_simple_gov <- aggte(out_gov, type = "simple", clustervars = "town_num", na.rm = T)

#________________________________
##to print
res_control <- c(round(es_simple_rebels$overall.att, 3), str_c("(", (round(es_simple_rebels$overall.se, 3)), ")"),
                 round(es_simple_gov$overall.att, 3), str_c("(", (round(es_simple_gov$overall.se, 3)), ")"),
                 round(es_simple_general$overall.att, 3), str_c("(", (round(es_simple_general$overall.se, 3)), ")"))


#________________________________
##For Model 3 Sample
rm(list = setdiff(ls(), c("elections", "res_control")))

#________________________________
#general attack
out_general <- att_gt(yname = "attacks_binary",
                      gname = "first_treat",
                      idname = "town_num",
                      tname = "week",
                      xformla = ~ 1,
                      data = elections[elections$to_keep_dur == 1,],
                      est_method = "reg",
                      clustervars = "town_num",
                      allow_unbalanced_panel = T
)

es_simple_general <- aggte(out_general, type = "simple", clustervars = "town_num", na.rm = T)

#________________________________
##rebels attack
out_rebels <- att_gt(yname = "rebels_binary",
                     gname = "first_treat",
                     idname = "town_num",
                     tname = "week",
                     xformla = ~ 1,
                     data = elections[elections$to_keep_dur == 1,],
                     est_method = "reg",
                     clustervars = "town_num",
                     allow_unbalanced_panel = T
)

es_simple_rebels <- aggte(out_rebels, type = "simple", clustervars = "town_num", na.rm = T)

#________________________________
##gov attack
out_gov <- att_gt(yname = "government_binary",
                  gname = "first_treat",
                  idname = "town_num",
                  tname = "week",
                  xformla = ~ 1,
                  data = elections[elections$to_keep_dur == 1,],
                  est_method = "reg",
                  clustervars = "town_num",
                  allow_unbalanced_panel = T
)

es_simple_gov <- aggte(out_gov, type = "simple", clustervars = "town_num", na.rm = T)

#________________________________
##to print
res_dur <- c(round(es_simple_rebels$overall.att, 3), str_c("(", (round(es_simple_rebels$overall.se, 3)), ")"),
             round(es_simple_gov$overall.att, 3), str_c("(", (round(es_simple_gov$overall.se, 3)), ")"),
             round(es_simple_general$overall.att, 3), str_c("(", (round(es_simple_general$overall.se, 3)), ")"))

#________________________________
##For Model 1 Sample
rm(list = setdiff(ls(), c("elections", "res_control", "res_dur")))


#________________________________
##general attack
out_general <- att_gt(yname = "attacks_binary",
                      gname = "first_treat",
                      idname = "town_num",
                      tname = "week",
                      xformla = ~ 1,
                      data = elections,
                      est_method = "reg",
                      clustervars = "town_num",
                      allow_unbalanced_panel = T
)

es_simple_general <- aggte(out_general, type = "simple", clustervars = "town_num", na.rm = T)

#________________________________
##rebels attack
out_rebels <- att_gt(yname = "rebels_binary",
                     gname = "first_treat",
                     idname = "town_num",
                     tname = "week",
                     xformla = ~ 1,
                     data = elections,
                     est_method = "reg",
                     clustervars = "town_num",
                     allow_unbalanced_panel = T
)

es_simple_rebels <- aggte(out_rebels, type = "simple", clustervars = "town_num", na.rm = T)

#________________________________
##gov attack
out_gov <- att_gt(yname = "government_binary",
                  gname = "first_treat",
                  idname = "town_num",
                  tname = "week",
                  xformla = ~ 1,
                  data = elections,
                  est_method = "reg",
                  clustervars = "town_num",
                  allow_unbalanced_panel = T
)

es_simple_gov <- aggte(out_gov, type = "simple", clustervars = "town_num", na.rm = T)

#To print
res_general <- c(round(es_simple_rebels$overall.att, 3), str_c("(", (round(es_simple_rebels$overall.se, 3)), ")"),
                 round(es_simple_gov$overall.att, 3), str_c("(", (round(es_simple_gov$overall.se, 3)), ")"),
                 round(es_simple_general$overall.att, 3), str_c("(", (round(es_simple_general$overall.se, 3)), ")"))

#________________________________
##Final table
to_print <- cbind(res_general, res_control, res_dur)
to_print <- rbind(to_print, c(nrow(elections), nrow(elections[elections$to_keep == 1,]), nrow(elections[elections$to_keep_dur == 1,])))
print(xtable(to_print), include.rownames = F)


#________________________________
##TABLE A4
rm(list = ls())
load("data/weekly_attack.RData")

#________________________________
##separate the treatment unit
treated <- filter(elections, first_treat > 0)
elections <- elections %>%
  group_by(city) %>%
  mutate(
    city_curfew = max(curfew), 
    city_to_keep = max(to_keep), 
    city_to_keep_dur = max(to_keep_dur)) %>%
  ungroup() %>%
  filter(first_treat == 0)

#________________________________
##get the neighbors
treated <- treated %>%
  group_by(city) %>%
  summarize(first_treat_city = min(first_treat))

#________________________________
##merge
elections <- left_join(elections, treated, by = "city")
elections$first_treat_city <- if_else(is.na(elections$first_treat_city), 0, elections$first_treat_city)

#________________________________
##SAMPLE FROM MODEL 2
#________________________________
##general attack 
out_general <- att_gt(yname = "attacks_binary",
                      gname = "first_treat_city",
                      idname = "town_num",
                      tname = "week",
                      xformla = ~ 1,
                      data = elections[elections$city_to_keep == 1,],
                      est_method = "reg",
                      clustervars = "town_num"
)
es_simple_general <- aggte(out_general, type = "simple", clustervars = "town_num")

#________________________________
#rebels attack
out_rebels <- att_gt(yname = "rebels_binary",
                     gname = "first_treat_city",
                     idname = "town_num",
                     tname = "week",
                     xformla = ~ 1,
                     data = elections[elections$city_to_keep == 1,],
                     est_method = "reg",
                     clustervars = "town_num"
)
es_simple_rebels <- aggte(out_rebels, type = "simple", clustervars = "town_num")

#________________________________
#gov attack
out_gov <- att_gt(yname = "government_binary",
                  gname = "first_treat_city",
                  idname = "town_num",
                  tname = "week",
                  xformla = ~ 1,
                  data = elections[elections$city_to_keep == 1,],
                  est_method = "reg",
                  clustervars = "town_num"
)
es_simple_gov <- aggte(out_gov, type = "simple", clustervars = "town_num")
res_control <- c(round(es_simple_rebels$overall.att, 3), str_c("(", (round(es_simple_rebels$overall.se, 3)), ")"),
                 round(es_simple_gov$overall.att, 3), str_c("(", (round(es_simple_gov$overall.se, 3)), ")"),
                 round(es_simple_general$overall.att, 3), str_c("(", (round(es_simple_general$overall.se, 3)), ")"))


#________________________________
##SAMPLE FROM MODEL 3
rm(list = setdiff(ls(), c("elections", "res_control")))

#________________________________
#general attack
out_general <- att_gt(yname = "attacks_binary",
                      gname = "first_treat_city",
                      idname = "town_num",
                      tname = "week",
                      xformla = ~ 1,
                      data = elections[elections$city_to_keep_dur == 1,],
                      est_method = "reg",
                      clustervars = "town_num"
)
es_simple_general <- aggte(out_general, type = "simple", clustervars = "town_num")

#________________________________
##rebels attack
out_rebels <- att_gt(yname = "rebels_binary",
                     gname = "first_treat_city",
                     idname = "town_num",
                     tname = "week",
                     xformla = ~ 1,
                     data = elections[elections$city_to_keep_dur == 1,],
                     est_method = "reg",
                     clustervars = "town_num"
)
es_simple_rebels <- aggte(out_rebels, type = "simple", clustervars = "town_num")

#________________________________
##gov attack
out_gov <- att_gt(yname = "government_binary",
                  gname = "first_treat_city",
                  idname = "town_num",
                  tname = "week",
                  xformla = ~ 1,
                  data = elections[elections$city_to_keep_dur == 1,],
                  est_method = "reg",
                  clustervars = "town_num"
)
es_simple_gov <- aggte(out_gov, type = "simple", clustervars = "town_num")

res_dur <- c(round(es_simple_rebels$overall.att, 3), str_c("(", (round(es_simple_rebels$overall.se, 3)), ")"),
             round(es_simple_gov$overall.att, 3), str_c("(", (round(es_simple_gov$overall.se, 3)), ")"),
             round(es_simple_general$overall.att, 3), str_c("(", (round(es_simple_general$overall.se, 3)), ")"))


#________________________________
##SAMPLE FROM MODEL 1
rm(list = setdiff(ls(), c("elections", "res_control", "res_dur")))

#________________________________
##general attack
out_general <- att_gt(yname = "attacks_binary",
                      gname = "first_treat_city",
                      idname = "town_num",
                      tname = "week",
                      xformla = ~ 1,
                      data = elections,
                      est_method = "reg",
                      clustervars = "town_num"
)
es_simple_general <- aggte(out_general, type = "simple", clustervars = "town_num")

#________________________________
##rebels attack
out_rebels <- att_gt(yname = "rebels_binary",
                     gname = "first_treat_city",
                     idname = "town_num",
                     tname = "week",
                     xformla = ~ 1,
                     data = elections,
                     est_method = "reg",
                     clustervars = "town_num"
)
es_simple_rebels <- aggte(out_rebels, type = "simple", clustervars = "town_num")

#________________________________
##gov attack
out_gov <- att_gt(yname = "government_binary",
                  gname = "first_treat_city",
                  idname = "town_num",
                  tname = "week",
                  xformla = ~ 1,
                  data = elections,
                  est_method = "reg",
                  clustervars = "town_num"
)
es_simple_gov <- aggte(out_gov, type = "simple", clustervars = "town_num")

res_general <- c(round(es_simple_rebels$overall.att, 3), str_c("(", (round(es_simple_rebels$overall.se, 3)), ")"),
                 round(es_simple_gov$overall.att, 3), str_c("(", (round(es_simple_gov$overall.se, 3)), ")"),
                 round(es_simple_general$overall.att, 3), str_c("(", (round(es_simple_general$overall.se, 3)), ")"))

#________________________________
##table to print
to_print <- cbind(res_general, res_control, res_dur)
to_print <- rbind(to_print, c(nrow(elections), nrow(elections[elections$city_to_keep == 1,]), nrow(elections[elections$city_to_keep_dur == 1,])))
print(xtable(to_print), include.rownames = F)



